﻿Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim image As New Bitmap(TextBox1.Text)
        Dim w As Int16 = image.Size.Width
        Dim h As Int16 = image.Size.Height
        'MsgBox("w:::::" & w & "/////h::::::" & h)
        If Int(TextBox2.Text) <= 0 Then TextBox2.Text = "1"
        If Int(TextBox3.Text) <= 0 Then TextBox3.Text = "1"
        Dim s_w As Int16 = Int(TextBox2.Text)
        Dim s_h As Int16 = Int(TextBox3.Text)

        Dim NewImageW As Int16 = Int(w / s_w)
        Dim NewImageH As Int16 = Int(h / s_h)

        Dim NImgPath As String
        Dim fName As String = PathFormate(TextBox1.Text, 3)
        Dim className As String = PathFormate(TextBox1.Text, 4)
        NImgPath = PathFormate(TextBox1.Text, 1)

        Dim rc As Rectangle = New Rectangle(0, 0, NewImageW, NewImageH)
        Dim NewImage As Bitmap
        For x = 0 To s_w - 1
            For y = 0 To s_h - 1
                rc = New Rectangle(x * NewImageW, y * NewImageH, NewImageW， NewImageH)
                NewImage = New Bitmap(image.Clone(rc, Imaging.PixelFormat.Format32bppArgb))
                NewImage.Save(NImgPath & fName & "_" & y & "_" & x & "." & className)
            Next
        Next
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.DragDrop


    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.AllowDrop = True
        TextBox2.Text = "1"
        TextBox3.Text = "1"
    End Sub
    Private Sub TextBoxDragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                Me.TextBox1.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If
    End Sub

    Private Function PathFormate(st As String, o As Int16) As String
        Dim newPath As String
        Dim sst As String
        sst = Replace(st, "/", "\")
        Dim s() As String = Split(sst, "\")
        Dim ns() As String
        newPath = s(0)
        Select Case o
            Case 1  '文件路径，不含文件名
                For i = 1 To UBound(s) - 1
                    newPath = newPath & "\" & s(i)
                Next
                newPath = newPath & "\"
            Case 2  '文件名，不含路径，含后缀
                newPath = s(UBound(s))
            Case 3 '文件名，不含路径，不含后缀
                ns = Split(s(UBound(s)), ".")
                newPath = ""
                For i = 0 To UBound(ns) - 1
                    newPath = newPath & ns(i) & "."
                Next
                newPath = Mid(newPath, 1, Len(newPath) - 1)
            Case 4 '后缀名
                ns = Split(s(UBound(s)), ".")
                newPath = ""
                newPath = ns(UBound(ns))
            Case Else
                newPath = st
        End Select
        Return newPath
    End Function

End Class
